package team.seekers.hula.record.history.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import team.seekers.hula.record.history.entity.dao.HistoryDO;
import team.seekers.hula.record.history.entity.dto.HistorySaveDTO;
import team.seekers.hula.record.history.entity.dto.HistoryUpdateDTO;
import team.seekers.hula.record.history.entity.dto.HistoryUserDTO;
import team.seekers.hula.record.history.entity.vo.HistoryInfoVO;
import team.seekers.hula.record.history.entity.vo.HistoryListVO;
import team.seekers.hula.record.history.entity.vo.HistoryUserVO;
import team.seekers.hula.record.history.mapper.HistoryMapper;
import team.seekers.hula.user.user.entity.vo.UserBasicInfoVO;
import team.seekers.hula.user.user.service.UserService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author hbc
 * @since 2022-11-22
 */
@Service
public class HistoryService extends ServiceImpl<HistoryMapper, HistoryDO> {

    @Resource
    private UserService userService;


    public String save(HistorySaveDTO saveDTO) {
        HistoryDO bonfireDO = new HistoryDO();
        BeanUtils.copyProperties(saveDTO,bonfireDO);
        save(bonfireDO);
        return bonfireDO.getId();
    }

    public void remove(String id) {
        removeById(id);
    }

    public HistoryInfoVO info(String id) {
        HistoryDO bonfireDO = getById(id);
        HistoryInfoVO historyInfoVO = new HistoryInfoVO();
        BeanUtils.copyProperties(bonfireDO,historyInfoVO);

        List<HistoryUserDTO> historyUserDTOS = JSON.parseArray(bonfireDO.getHistoryUsers(), HistoryUserDTO.class);

        ArrayList<String> userIds = new ArrayList<>();
        for (HistoryUserDTO historyUserDTO : historyUserDTOS) {
            userIds.add(historyUserDTO.getUserId());
        }

        Map<String, UserBasicInfoVO> map = userService.idsMapBasicInfoVOList(userIds);

        ArrayList<HistoryUserVO> historyUserVOS = new ArrayList<>();
        for (HistoryUserDTO historyUserDTO : historyUserDTOS) {
            HistoryUserVO historyUserVO = new HistoryUserVO();
            BeanUtils.copyProperties(historyUserDTO,historyUserVO);
            historyUserVO.setUserInfo(map.get(historyUserDTO.getUserId()));
            historyUserVOS.add(historyUserVO);
        }

        historyInfoVO.setHistoryUsers(historyUserVOS);
        return historyInfoVO;
    }

    public void update(HistoryUpdateDTO updateVO) {
        HistoryDO bonfireDO = new HistoryDO();
        BeanUtils.copyProperties(updateVO,bonfireDO);
        updateById(bonfireDO);
    }

    public List<HistoryListVO> listVO(){
        List<HistoryDO> list = list();
        ArrayList<HistoryListVO> bonfireListVOS = new ArrayList<>();

        for (HistoryDO bonfireDO : list) {
            HistoryListVO bonfireListVO = new HistoryListVO();
            BeanUtils.copyProperties(bonfireDO,bonfireListVO);
            bonfireListVOS.add(bonfireListVO);
        }

        return bonfireListVOS;
    }
}
